import { ActionName } from '@/napcat-onebot/action/router';
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
import { MessageUnique } from 'napcat-common/src/message-unique';
import { Static, Type } from '@sinclair/typebox';

const SchemaData = Type.Object({
  message_id: Type.Union([Type.Number(), Type.String()]),
});

type Payload = Static<typeof SchemaData>;

class DeleteMsg extends OneBotAction<Payload, void> {
  override actionName = ActionName.DeleteMsg;
  override payloadSchema = SchemaData;

  async _handle (payload: Payload) {
    const msg = MessageUnique.getMsgIdAndPeerByShortId(Number(payload.message_id));
    if (msg) {
      this.obContext.recallEventCache.set(msg.MsgId, setTimeout(() => {
        this.obContext.recallEventCache.delete(msg.MsgId);
      }, 5000));
      await this.core.apis.MsgApi.recallMsg(msg.Peer, msg.MsgId);
    } else {
      throw new Error('Recall failed');
    }
  }
}

export default DeleteMsg;
